package com.yycx.module.bus.provider.handler.cbreturn;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.SaveInterceptor;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.utils.ConvertUtils;
import com.yycx.common.utils.OrderNoUtil;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 退保证金保存扩展器
 *
 * @author zyf
 */
@Component("cbReturnPaymentSaveHandler")
public class CbReturnPaymentSaveHandler implements SaveInterceptor<BusPayment> {


    @Autowired
    private BusDeptService deptService;

    @Autowired
    private BusOrderDetailsService orderDetailsService;

    @Autowired
    private BusOrderService orderService;
    @Resource
    private OrderNoUtil orderNoUtil;


    @Override
    public void prepare(CriteriaSave cs, EntityMap entityMap, BusPayment payment) {
        Long orderDetailsId = cs.getLong("orderDetailsId");
        //如果是批量退款
        String orderDetailsIds = cs.getParams("selectIds");
        ApiAssert.anyOneIsNotNull("结算明细不能为空", orderDetailsId, orderDetailsIds);

        Long deptId = payment.getDeptId();
        ApiAssert.isNotEmpty("deptId能为空", deptId);
        BusDept dept = deptService.getById(deptId);
        payment.setTitle("退市场资保金");
        payment.setAmountType(AmountTypeEnum.CbReturn.getCode());
        payment.setOrganizationId(dept.getOrganizationId());
        payment.setCompanyId(dept.getCompanyId());
        payment.setUserId(OpenHelper.getUserId());
        payment.setNickName(OpenHelper.getNickName());
        payment.setDeptId(dept.getDeptId());
        if (FlymeUtils.isNotEmpty(orderDetailsId)) {
            //应退保证金
            BigDecimal returnCustomBond = payment.getAmount();
            //单个退款
            BusOrderDetails orderDetails = orderDetailsService.getById(orderDetailsId);
            Long orderId = orderDetails.getOrderId();
            BusOrder order = orderService.getById(orderId);
            payment.setSupplierId(order.getSupplierId());
            payment.setCustomId(orderDetails.getCustomId().toString());
            payment.setOrderId(orderDetails.getOrderId().toString());
            payment.setOrderDetailsId(orderDetails.getOrderDetailsId().toString());
            payment.setPaymentNo("TB" + order.getOrderNo());
            payment.setAmount(returnCustomBond);
        }
        if(FlymeUtils.isNotEmpty(orderDetailsIds)){
            Integer orderCount= orderDetailsService.countByOrderDetails(orderDetailsIds);
            String[] ids=orderDetailsIds.split(",");
            if(orderCount==1){
                BusOrderDetails orderDetails = orderDetailsService.getById(ids[0]);
                Long orderId = orderDetails.getOrderId();
                BusOrder order = orderService.getById(orderId);
                payment.setSupplierId(order.getSupplierId());
                payment.setCustomId(orderDetails.getCustomId().toString());
                payment.setOrderId(orderDetails.getOrderId().toString());
                payment.setCustomId(orderDetails.getCustomId().toString());
                payment.setOrderDetailsId(orderDetails.getOrderDetailsId().toString());
                payment.setPaymentNo("TB" + order.getOrderNo());
            }
            payment.setPaymentNo("TB"+orderNoUtil.getIncrementNum());
            payment.setAmount(payment.getAmount());

        }
        payment.setPaymentState(CommonConstants.INT_0);
    }

    @Override
    public void complete(CriteriaSave cs, EntityMap params, BusPayment entity) {
        Long orderDetailsId = cs.getLong("orderDetailsId");
        String orderDetailsIds = cs.getParams("selectIds");
        List<String> ids=new ArrayList();
        if(FlymeUtils.isNotEmpty(orderDetailsIds)){
            ids= ConvertUtils.stringTolist(orderDetailsIds);
        }else{
            ids.add(orderDetailsId.toString());
        }
        for (String id : ids) {
            //更新的退保证金ID
            orderDetailsService.updateCbReturn(entity, Long.parseLong(id));
        }

    }

}
